package com.avic.rong.shoppingCar.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.avic.rong.BaseActivity;
import com.avic.rong.MainActivity;
import com.avic.rong.R;
import com.avic.rong.activity.login.LoginActivity;
import com.avic.rong.activity.login.LoginPhoneActivity;
import com.avic.rong.adapter.MineLikeAdapter;
import com.avic.rong.bean.GoCartListEvent;
import com.avic.rong.shoppingCar.adapter.ShoppingCarRvAdapter;
import com.avic.rong.shoppingCar.api.ShoppingCarBatchAddUserFavoritesApi;
import com.avic.rong.shoppingCar.api.ShoppingCarChangeSkuApi;
import com.avic.rong.shoppingCar.api.ShoppingCarDelCartApi;
import com.avic.rong.shoppingCar.api.ShoppingCarGoodsKuCunApi;
import com.avic.rong.shoppingCar.api.ShoppingCarListApi;
import com.avic.rong.shoppingCar.api.ShoppingCarListnoLoginApi;
import com.avic.rong.shoppingCar.api.ShoppingCarUpDateCartNumApi;
import com.avic.rong.shoppingCar.bean.ShoppingCarListBean;
import com.avic.rong.userOrder.activity.OrderConfirmActivity;
import com.avic.rong.userOrder.api.CaiLikeApi;
import com.avic.rong.userOrder.bean.CaiLikeBean;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.DensityUtils;
import com.avic.rong.utils.DeviceUtils;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.avic.rong.utils.StringUtils;
import com.avic.rong.utils.ToastTools;
import com.avic.rong.wedgit.loadingLayout.LoadingLayout;
import com.bumptech.glide.Glide;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.luck.picture.lib.decoration.GridSpacingItemDecoration;

import org.greenrobot.eventbus.EventBus;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ShoppingCarActivity extends BaseActivity implements View.OnClickListener, ShoppingCarRvAdapter.DeleteItem {

    @BindView(R.id.shoppingCar_Title_back)
    ImageView shoppingCarTitleBack;
    @BindView(R.id.shoppingCar_Title_Tv)
    TextView shoppingCarTitleTv;
    @BindView(R.id.shoppingCar_Title_BianJi)
    TextView shoppingCarTitleBianJi;
    @BindView(R.id.shoppingCar_Title_Dot)
    ImageView shoppingCarTitleDot;

    @BindView(R.id.shoppingCar_ConstraintLayout_Title)
    ConstraintLayout shoppingCarConstraintLayoutTitle;
    @BindView(R.id.shoppingCar_TopDL_DengLu)
    TextView shoppingCarTopDLDenglu;
    @BindView(R.id.shoppingCar_RelativeLayout_TopDL)
    LinearLayout shoppingCarRelativeLayoutTopDL;

    @BindView(R.id.shoppingCar_WeiDL_Img)
    ImageView shoppingCarWeiDLImg;
    @BindView(R.id.shoppingCar_WeiDL_Tv)
    TextView shoppingCarWeiDLTv;
    @BindView(R.id.shoppingCar_WeiDL_DengLu)
    TextView shoppingCarWeiDLDengLu;
    @BindView(R.id.shoppingCar_WeiDL_QuGouWu)
    TextView shoppingCarWeiDLQuGouWu;
    @BindView(R.id.shoppingCar_ConstraintLayout_WeiDL)
    ConstraintLayout shoppingCarConstraintLayoutWeiDL;
    @BindView(R.id.shoppingCar_GoodsRv)
    RecyclerView shoppingCarGoodsRv;

    @BindView(R.id.shoppingCar_WeiDL_CaiLove)
    TextView shoppingCarWeiDLCaiLove;
    @BindView(R.id.shoppingCar_CaiLove_Rv)
    RecyclerView shoppingCarCaiLoveRv;
    @BindView(R.id.shoppingCar_ConstraintLayout_CaiLove)
    ConstraintLayout shoppingCarConstraintLayoutCaiLove;
    @BindView(R.id.shoppingCar_BottomView_JieSuan_JieSuan)
    TextView shoppingCarBottomViewJieSuanJieSuan;
    @BindView(R.id.shoppingCar_BottomView_JieSuan_checkBox)
    CheckBox shoppingCarBottomViewJieSuanCheckBox;
    @BindView(R.id.shoppingCar_BottomView_JieSuan_Price)
    TextView shoppingCarBottomJieSuanViewPrice;
    //结算View
    @BindView(R.id.shoppingCar_BottomView_JieSuan_ConstraintLayout)
    ConstraintLayout shoppingCarBottomViewJieSuanConstraintLayout;
    @BindView(R.id.shoppingCar_BottomView_CaoZuo_SellectAll)
    CheckBox shoppingCarBottomViewCaoZuoSellectAll;
    @BindView(R.id.shoppingCar_BottomView_CaoZuo_Delete)
    TextView shoppingCarBottomViewCaoZuoDelete;
    @BindView(R.id.shoppingCar_BottomView_CaoZuo_SouCang)
    TextView shoppingCarBottomViewCaoZuoSouCang;
    //操作View
    @BindView(R.id.shoppingCar_ConstraintLayout_BottomView_CaoZuo)
    ConstraintLayout shoppingCarConstraintLayoutBottomViewCaoZuo;
    @BindView(R.id.loading_layout)
    LoadingLayout loading_Layout;
    @BindView(R.id.biaoshi_wenzi)
    TextView biaoshiWenzi;
    private PopupWindow popupWindow;
    private ShoppingCarRvAdapter Goodsadapter;
    private ShoppingCarListBean bean = new ShoppingCarListBean();
    private List<ShoppingCarListBean.CartListBean> cartList;
    private TextView sku_add;
    private TextView sku_dianzi;
    private TextView sku_money;
    private TextView sku_num;
    private TextView sku_remove;
    private TextView sku_type;
    private TextView xiaoguo;
    private ImageView sku_img;
    private TextView skuPop_true;
    private TextView skuPop_false;
    private TextView text_car_sku_source;
    private TextView text_car_sku_no;
    private LinearLayout no_image_layout;
    private ArrayList<CaiLikeBean> listData = new ArrayList<>();
    private TextView sku_zhizhi;
    private TextView sku_zonghe;
    private ImageView sku_close;
    private String skuName;
    private String skuName2;
    private String skuName3;
    private boolean islogin = true;
    private String realskuName;
    private TextView kucunsize;
    private TextView sku_title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopping_car);
        ButterKnife.bind(this);

        shoppingCarGoodsRv.setNestedScrollingEnabled(false);
        shoppingCarCaiLoveRv.setNestedScrollingEnabled(false);
        initListener();
        loadData();
        initData();

    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_shopping_car;
    }

    @Override
    protected void initView() {
    }

    private void setViewVisible() {
        boolean login = SharedPreferencesUtil.isLogin();
        if (login) {
            //操作
            shoppingCarRelativeLayoutTopDL.setVisibility(View.GONE);
            biaoshiWenzi.setVisibility(View.GONE);
            shoppingCarTopDLDenglu.setVisibility(View.GONE);
            shoppingCarConstraintLayoutBottomViewCaoZuo.setVisibility(View.GONE);
            if (cartList.size() > 0) {
                //结算
                shoppingCarBottomViewJieSuanConstraintLayout.setVisibility(View.VISIBLE);
                //未登录且没有数据
                shoppingCarConstraintLayoutWeiDL.setVisibility(View.GONE);
            } else {
                //结算
                shoppingCarBottomViewJieSuanConstraintLayout.setVisibility(View.GONE);
                //未登录
                shoppingCarConstraintLayoutWeiDL.setVisibility(View.VISIBLE);
                shoppingCarWeiDLTv.setText("目前没有商品，去购物吧");
                shoppingCarWeiDLDengLu.setVisibility(View.GONE);
                shoppingCarTitleBianJi.setVisibility(View.GONE);
            }
        } else {
            shoppingCarTitleBianJi.setVisibility(View.GONE);
            shoppingCarWeiDLTv.setText("登录后可同步购物车中的商品");
            shoppingCarWeiDLDengLu.setVisibility(View.VISIBLE);
            //操作
            shoppingCarConstraintLayoutBottomViewCaoZuo.setVisibility(View.GONE);
            if (cartList.size() > 0) {
                shoppingCarRelativeLayoutTopDL.setVisibility(View.VISIBLE);
                biaoshiWenzi.setVisibility(View.VISIBLE);
                shoppingCarTopDLDenglu.setVisibility(View.VISIBLE);
                //结算
                shoppingCarBottomViewJieSuanConstraintLayout.setVisibility(View.VISIBLE);
                //未登录
                shoppingCarConstraintLayoutWeiDL.setVisibility(View.GONE);
            } else {
                //结算
                shoppingCarBottomViewJieSuanConstraintLayout.setVisibility(View.GONE);
                //未登录
                shoppingCarConstraintLayoutWeiDL.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    protected void addListener() {}

    private void initData() {
        shoppingCarGoodsRv.setLayoutManager(new LinearLayoutManager(this));
        Goodsadapter = new ShoppingCarRvAdapter(this);
        Goodsadapter.setDeleteItem(this);
        shoppingCarGoodsRv.setAdapter(Goodsadapter);
        Map<String, String> map = new HashMap<>();
        map.put("token", getSharedPreferencesUtil().getLogintoken() + "");
        final CaiLikeApi caiLikeApi = new CaiLikeApi(this, simpleOnNextListenerCaiLike);
        caiLikeApi.setCache(false);
        caiLikeApi.setMap(map);
        HttpManager.getInstance().doHttpDeal(caiLikeApi);
    }

    //猜你需要
    HttpOnNextListener<BaseResultEntity> simpleOnNextListenerCaiLike = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            listData.clear();
            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                ToastTools.showToast(ShoppingCarActivity.this, subjects.getMsg());
                return;
            }
            GridLayoutManager gridLayoutManager = new GridLayoutManager(ShoppingCarActivity.this, 2);
            shoppingCarCaiLoveRv.addItemDecoration(new GridSpacingItemDecoration(2, DensityUtils.dip2px(ShoppingCarActivity.this,10),false));
            shoppingCarCaiLoveRv.setLayoutManager(gridLayoutManager);
            listData.addAll((ArrayList<CaiLikeBean>) subjects.getData());
            MineLikeAdapter mineLikeAdapter = new MineLikeAdapter(ShoppingCarActivity.this, listData);
            shoppingCarCaiLoveRv.setAdapter(mineLikeAdapter);
        }
    };

    private void initListener() {
        //TopView
        shoppingCarTitleBack.setOnClickListener(this);
        shoppingCarTitleBianJi.setOnClickListener(this);
        shoppingCarTitleDot.setOnClickListener(this);
        //TopDL
        shoppingCarTopDLDenglu.setOnClickListener(this);
        //WeiDL
        shoppingCarWeiDLDengLu.setOnClickListener(this);
        shoppingCarWeiDLQuGouWu.setOnClickListener(this);
        //BottomViewJieSuan
        shoppingCarBottomViewJieSuanCheckBox.setOnClickListener(this);

        shoppingCarBottomViewCaoZuoSellectAll.setOnClickListener(this);
        //BottomViewCaoZuo
        shoppingCarBottomViewCaoZuoSouCang.setOnClickListener(this);
        shoppingCarBottomViewCaoZuoDelete.setOnClickListener(this);

        //结算
        shoppingCarBottomViewJieSuanJieSuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (SharedPreferencesUtil.isLogin()) {
                    //获取sku和num拼在一起
                    String skuNum = "";
                    for (int i = 0; i < cartList.size(); i++) {
                        if (cartList.get(i).isIsSelect()) {
                            int skuId = cartList.get(i).getSkuId();
                            int goodsNumber = cartList.get(i).getGoodsNumber();
                            skuNum += skuId + ":" + goodsNumber + ",";
                        }
                    }
                    String skuOk = "";
                    if (!skuNum.equals("")) {
                        String substring = skuNum.substring(0, skuNum.length() - 1);
                        skuOk = "{" + substring + "}";
                    } else {
                        ToastTools.showToast(ShoppingCarActivity.this, "请选中商品");
                    }

                    if (!skuOk.equals("")) {
                        Intent intent = new Intent(ShoppingCarActivity.this, OrderConfirmActivity.class);
                        intent.putExtra("skuStr", skuOk);
                        intent.putExtra("enterType", "1");
                        startActivity(intent);
                    }
                } else {
                    startActivity(new Intent(ShoppingCarActivity.this, LoginPhoneActivity.class));
                }


            }
        });


    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.shoppingCar_Title_back:
                this.finish();
                break;
            case R.id.shoppingCar_Title_BianJi:
                if (shoppingCarTitleBianJi.getText().equals("编辑")) {
                    setListFalse();
                    shoppingCarBottomViewCaoZuoSellectAll.setSelected(false);
                    //编辑
                    shoppingCarTitleBianJi.setVisibility(View.VISIBLE);
                    shoppingCarTitleBianJi.setText("完成");
                    shoppingCarBottomViewJieSuanConstraintLayout.setVisibility(View.GONE);
                    shoppingCarConstraintLayoutBottomViewCaoZuo.setVisibility(View.VISIBLE);
                } else {
                    shoppingCarTitleBianJi.setText("编辑");
                    //操作
                    shoppingCarConstraintLayoutBottomViewCaoZuo.setVisibility(View.GONE);
                    //结算
                    shoppingCarBottomViewJieSuanConstraintLayout.setVisibility(View.VISIBLE);


                    Goodsadapter.notifyDataSetChanged();
                }

                break;
            case R.id.shoppingCar_Title_Dot:
                showPopup(ShoppingCarActivity.this, shoppingCarConstraintLayoutTitle);
                break;
            case R.id.shoppingCar_TopDL_DengLu:
                Intent intent = new Intent(this, LoginActivity.class);
                startActivity(intent);
                break;
            case R.id.shoppingCar_WeiDL_QuGouWu:
                Intent intent1 = new Intent(this, MainActivity.class);
                EventBus.getDefault().postSticky(new GoCartListEvent());
                startActivity(intent1);
                break;
            case R.id.shoppingCar_BottomView_JieSuan_checkBox:
                if (shoppingCarBottomViewJieSuanCheckBox.isSelected()) {
                    setListFalse();
                    shoppingCarBottomViewJieSuanCheckBox.setSelected(false);
                } else {
                    setListTrue();
                    shoppingCarBottomViewJieSuanCheckBox.setSelected(true);
                }
                break;
            case R.id.shoppingCar_BottomView_JieSuan_JieSuan:

                break;
            case R.id.shoppingCar_BottomView_CaoZuo_SellectAll:
                if (!shoppingCarBottomViewCaoZuoSellectAll.isSelected()) {
                    setListTrue();
                    shoppingCarBottomViewCaoZuoSellectAll.setSelected(true);
                } else {
                    setListFalse();
                    shoppingCarBottomViewCaoZuoSellectAll.setSelected(false);
                }
                break;
            case R.id.shoppingCar_BottomView_CaoZuo_Delete:
                boolean dele = isSelectgoods();
                if (dele) {
                    showDialogDelete();
                } else {
                    ToastTools.showToast(ShoppingCarActivity.this, "请选中商品");
                }

                break;
            case R.id.shoppingCar_BottomView_CaoZuo_SouCang:

                boolean selectgoods = isSelectgoods();
                if (selectgoods) {
                    showDialogCollect();
                } else {
                    ToastTools.showToast(ShoppingCarActivity.this, "请选中商品");
                }
                break;
        }
    }

    //收藏商品dialog
    private void showDialogCollect() {
        View inflate = LayoutInflater.from(ShoppingCarActivity.this).inflate(R.layout.dialog_collect, null, false);
        final AlertDialog alertDialog = new AlertDialog.Builder(this).setView(inflate).show();
        TextView dialogColleteCancel = inflate.findViewById(R.id.dialog_collect_cancel);
        TextView dialogColleteShanChu = inflate.findViewById(R.id.dialog_collect_ok);
        dialogColleteCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.cancel();
            }
        });
        dialogColleteShanChu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Integer> skuList = new ArrayList<>();
                List<Integer> cartIdList = new ArrayList<>();
                for (int i = 0; i < cartList.size(); i++) {
                    if (cartList.get(i).isIsSelect()) {
                        skuList.add(cartList.get(i).getSkuId());
                        cartIdList.add(cartList.get(i).getCartId());

                    }
                }

                    batchAddUserFavorites(1, skuList,cartIdList);
                alertDialog.cancel();
            }
        });
    }

    //删除商品dialog
    private void showDialogDelete() {
        View inflate = LayoutInflater.from(ShoppingCarActivity.this).inflate(R.layout.dialog_delete, null, false);
        final AlertDialog alertDialog = new AlertDialog.Builder(this).setView(inflate).show();
        TextView dialogDeleteCancel = inflate.findViewById(R.id.dialog_delete_cancel);
        TextView dialogDeleteShanchu = inflate.findViewById(R.id.dialog_delete_shanchu);
        dialogDeleteCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.cancel();
            }
        });
        dialogDeleteShanchu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Integer> integerList = new ArrayList<>();
                for (int i = 0; i < cartList.size(); i++) {
                    if (cartList.get(i).isIsSelect()) {
                        integerList.add(cartList.get(i).getCartId());
                    }
                }
                delCart(integerList);
                for (int i = 0; i < cartList.size(); i++) {
                    if (cartList.get(i).isIsSelect()) {
                        cartList.remove(i);
                        i = -1;
                    }
                }
                Goodsadapter.notifyDataSetChanged();

                alertDialog.cancel();
            }
        });
    }

    //设置购物车列表全不选
    private void setListFalse() {
        for (int i = 0; i < cartList.size(); i++) {
            cartList.get(i).setIsSelect(false);
            Goodsadapter.notifyDataSetChanged();
        }
    }

    //设置购物车列表全选
    private void setListTrue() {
        for (int i = 0; i < cartList.size(); i++) {
            cartList.get(i).setIsSelect(true);
            Goodsadapter.notifyDataSetChanged();
        }
    }

    @Override
    public void delete(ShoppingCarListBean.CartListBean.GoodsBean goodsBean, int index) {
        List<Integer> listdelete = new ArrayList<>();
        listdelete.add(cartList.get(index).getCartId());
        delCart(listdelete);
        if (cartList != null) {
            cartList.remove(index);
        }
        Goodsadapter.notifyDataSetChanged();

    }

    @Override
    public void collect(ShoppingCarListBean.CartListBean.GoodsBean goodsBean, int index) {
        if (!SharedPreferencesUtil.isLogin()) {
            startActivity(LoginPhoneActivity.queryIntent(ShoppingCarActivity.this));
            return;
        }
        List<Integer> listcollect = new ArrayList<>();
        List<Integer> listcontId = new ArrayList<>();
        listcollect.add(goodsBean.getSkuId());
        listcontId.add(cartList.get(index).getCartId());
        batchAddUserFavorites(6, listcollect,listcontId);
        cartList.remove(index);
        Goodsadapter.notifyDataSetChanged();


    }


    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void skuPop(int index) {
        View view = (View) LayoutInflater.from(this).inflate(R.layout.view_goods_sku, null);
        sku_add = view.findViewById(R.id.sku_add);
        sku_dianzi = view.findViewById(R.id.sku_dianzi);
        sku_money = view.findViewById(R.id.sku_money);
        sku_num = view.findViewById(R.id.sku_num);
        sku_title = view.findViewById(R.id.sku_title);
        sku_close = view.findViewById(R.id.sku_close);
        kucunsize = view.findViewById(R.id.kucunsize);
        sku_remove = view.findViewById(R.id.sku_remove);
        sku_zhizhi = view.findViewById(R.id.sku_zhizhi);
         sku_zonghe = view.findViewById(R.id.sku_zonghe);
        sku_type = view.findViewById(R.id.sku_type);
        xiaoguo = view.findViewById(R.id.xiaoguo);
        sku_img = view.findViewById(R.id.sku_img);
        skuPop_true = view.findViewById(R.id.skuPop_true);
        skuPop_false = view.findViewById(R.id.skuPop_false);
        text_car_sku_no = view.findViewById(R.id.text_car_sku_no);
        text_car_sku_source = view.findViewById(R.id.text_car_sku_source);
        no_image_layout = view.findViewById(R.id.no_image_layout);
        //获取选种商品的一些属性
        int num = cartList.get(index).getGoodsNumber();
        sku_title.setText(cartList.get(index).getProName());
        String image = cartList.get(index).getGoods().getGoodsImg();
        int skuId = cartList.get(index).getSkuId();
        if (cartList.get(index).getGoods().getGoodsSkuList().size()>0) {
            for (int k = 0; k < cartList.get(index).getGoods().getGoodsSkuList().size(); k++) {
                int skuListId = cartList.get(index).getGoods().getGoodsSkuList().get(k).getSkuId();
                if (skuId==skuListId){
                    realskuName = cartList.get(index).getGoods().getGoodsSkuList().get(k).getSkuName();
                    double shopPrice = cartList.get(index).getGoods().getGoodsSkuList().get(k).getShopPrice();
                    sku_type.setText("已选： " + realskuName);
                    int kucun = cartList.get(index).getGoods().getGoodsSkuList().get(k).getGoodsNumber();
                    kucunsize.setText(kucun+"");
                    sku_money.setText("¥" + shopPrice + "");
                }
            }
        }
        sku_num.setText(num + "");

        if (!StringUtils.isEmpty((CharSequence) cartList.get(index).getGoods().getAttrShow())){
            xiaoguo.setVisibility(View.VISIBLE);
            xiaoguo.setText(cartList.get(index).getGoods().getAttrShow().toString());
        }else{
            xiaoguo.setVisibility(View.INVISIBLE);
        }
        if (!StringUtils.isEmpty(cartList.get(index).getGoods().getGoodsImg())) {
            sku_img.setVisibility(View.VISIBLE);
            no_image_layout.setVisibility(View.GONE);
            if (cartList.get(index).getGoods().getGoodsImg().contains("rongrong.")) {
                Glide.with(view).load(image).into(sku_img);
            } else {
                Glide.with(context).load(bean.getBiaoZhunImg() + cartList.get(index).getGoods().getGoodsImg()).into(sku_img);
            }
        }else{
            sku_img.setVisibility(View.GONE);
            no_image_layout.setVisibility(View.VISIBLE);
            text_car_sku_no.setText(cartList.get(index).getGoods().getCatName());
            text_car_sku_source.setText(cartList.get(index).getGoods().getKeywords());
        }



        if (cartList.get(index).getGoods().getGoodsSkuList().size() == 3) {
            skuName = cartList.get(index).getGoods().getGoodsSkuList().get(0).getSkuName();
            skuName2 = cartList.get(index).getGoods().getGoodsSkuList().get(1).getSkuName();
            skuName3 = cartList.get(index).getGoods().getGoodsSkuList().get(2).getSkuName();
            sku_dianzi.setText(skuName2);
            sku_zhizhi.setText(skuName);
            sku_zonghe.setText(skuName3);
        }else if (cartList.get(index).getGoods().getGoodsSkuList().size() == 2){
            skuName = cartList.get(index).getGoods().getGoodsSkuList().get(0).getSkuName();
            skuName2 = cartList.get(index).getGoods().getGoodsSkuList().get(1).getSkuName();
            sku_dianzi.setText(skuName2);
            sku_zhizhi.setText(skuName);
            sku_zonghe.setVisibility(View.GONE);
        } else if (cartList.get(index).getGoods().getGoodsSkuList().size() == 1) {
            skuName = cartList.get(index).getGoods().getGoodsSkuList().get(0).getSkuName();
            sku_zhizhi.setText(skuName);
            sku_dianzi.setVisibility(View.GONE);
            sku_zonghe.setVisibility(View.GONE);
        }
        if (realskuName.equals(skuName)) {
            sku_zhizhi.setBackgroundResource(R.drawable.button_green_r25_xian);
            sku_zhizhi.setTextColor(getColor(R.color.FFADDA41));
        }
        if (realskuName.equals(skuName2)) {
            sku_dianzi.setBackgroundResource(R.drawable.button_green_r25_xian);
            sku_dianzi.setTextColor(getColor(R.color.FFADDA41));
        }
        if (realskuName.equals(skuName3)) {
            sku_zonghe.setBackgroundResource(R.drawable.button_green_r25_xian);
            sku_zonghe.setTextColor(getColor(R.color.FFADDA41));

        }

        commonPop(view);
        sku_zhizhi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sku_zhizhi.setBackgroundResource(R.drawable.button_green_r25_xian);
                sku_dianzi.setBackgroundResource(R.drawable.shape_goods_sku_gray);
                sku_zonghe.setBackgroundResource(R.drawable.shape_goods_sku_gray);
                sku_zhizhi.setTextColor(getColor(R.color.FFADDA41));
                sku_dianzi.setTextColor(Color.BLACK);
                sku_zonghe.setTextColor(Color.BLACK);
                int kucun = cartList.get(index).getGoods().getGoodsSkuList().get(0).getGoodsNumber();
                kucunsize.setText(kucun+"");
                int skuId = cartList.get(index).getGoods().getGoodsSkuList().get(0).getSkuId();
                sku_money.setText("¥"+cartList.get(index).getGoods().getGoodsSkuList().get(0).getShopPrice());
                cartList.get(index).setSkuId(skuId);
                int cartId = cartList.get(index).getCartId();
                if (skuName != null) {
                    sku_type.setText("已选：" + skuName);
                    changeSku(cartId, skuId);
                } else {

                }


            }
        });

        sku_dianzi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sku_zhizhi.setBackgroundResource(R.drawable.shape_goods_sku_gray);
                sku_dianzi.setBackgroundResource(R.drawable.button_green_r25_xian);
                sku_zonghe.setBackgroundResource(R.drawable.shape_goods_sku_gray);
                sku_zhizhi.setTextColor(Color.BLACK);
                sku_dianzi.setTextColor(getColor(R.color.FFADDA41));
                sku_zonghe.setTextColor(Color.BLACK);
                int kucun = cartList.get(index).getGoods().getGoodsSkuList().get(1).getGoodsNumber();
                kucunsize.setText(kucun+"");
                int skuId = cartList.get(index).getGoods().getGoodsSkuList().get(1).getSkuId();
                sku_money.setText("¥"+cartList.get(index).getGoods().getGoodsSkuList().get(1).getShopPrice());
                cartList.get(index).setSkuId(skuId);
                int cartId = cartList.get(index).getCartId();
                sku_type.setText("已选：" + skuName2);
                changeSku(cartId, skuId);
            }
        });

        sku_zonghe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sku_zhizhi.setBackgroundResource(R.drawable.shape_goods_sku_gray);
                sku_dianzi.setBackgroundResource(R.drawable.shape_goods_sku_gray);
                sku_zonghe.setBackgroundResource(R.drawable.button_green_r25_xian);
                sku_zhizhi.setTextColor(Color.BLACK);
                sku_dianzi.setTextColor(Color.BLACK);
                sku_zonghe.setTextColor(getColor(R.color.FFADDA41));
                int kucun = cartList.get(index).getGoods().getGoodsSkuList().get(2).getGoodsNumber();
                kucunsize.setText(kucun+"");
                int skuId = cartList.get(index).getGoods().getGoodsSkuList().get(2).getSkuId();
                sku_money.setText("¥"+cartList.get(index).getGoods().getGoodsSkuList().get(2).getShopPrice());
                cartList.get(index).setSkuId(skuId);
                int cartId = cartList.get(index).getCartId();
                sku_type.setText("已选：" + skuName2);
                changeSku(cartId, skuId);
            }
        });


        //关闭
        sku_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        //确认
        skuPop_true.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String trim = sku_type.getText().toString().trim();
                cartList.get(index).getGoods().setSkuName("标准载体： " + trim.substring(3, trim.length()));
                String s = sku_num.getText().toString();
                int i = Integer.parseInt(s);
                cartList.get(index).setGoodsNumber(i);
                popupWindow.dismiss();
                Goodsadapter.notifyDataSetChanged();
            }
        });

        //加
        sku_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferencesUtil.getInstance().put("index", index);
                String s = sku_num.getText().toString();
                int i = Integer.parseInt(s);
                int skuId = cartList.get(index).getSkuId();
                getKuCun(i, skuId);
            }
        });
        //减
        sku_remove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = sku_num.getText().toString();
                int i = Integer.parseInt(s);
                if (i > 1) {
                    i--;
                } else {
                    ToastTools.showToast(ShoppingCarActivity.this, "最少为1");
                }

                sku_num.setText(i + "");
                int cartId = cartList.get(index).getCartId();
                updateCartNum(i, cartId);
            }
        });

    }

    //获取商品库存
    private void getKuCun(int num, int skuId) {
        ShoppingCarGoodsKuCunApi goodsKuCunApi = new ShoppingCarGoodsKuCunApi((ShoppingCarActivity) context, httpOnNextListener, num, skuId);
        goodsKuCunApi.setCache(false);
        HttpManager.getInstance().doHttpDeal(goodsKuCunApi);
    }

    //库存
    HttpOnNextListener httpOnNextListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            int i = SharedPreferencesUtil.getInstance().getAsInt("index", 0);
            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                Toast.makeText(context, "商品库存不足", Toast.LENGTH_SHORT).show();

            } else {
                int cartId = cartList.get(i).getCartId();
                String s = sku_num.getText().toString();
                int num = Integer.parseInt(s);
                num++;
                sku_num.setText(num + "");
                updateCartNum(num, cartId);

            }
        }


        @Override
        public void onCacheNext(String cache) {


        }


        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", e.getMessage());
        }
    };

    @Override
    public void longClick() {
        //编辑
        shoppingCarTitleBianJi.setVisibility(View.VISIBLE);
        shoppingCarTitleBianJi.setText("完成");
        shoppingCarBottomViewJieSuanConstraintLayout.setVisibility(View.GONE);
        shoppingCarConstraintLayoutBottomViewCaoZuo.setVisibility(View.VISIBLE);
    }

    @Override
    public void showPrice(double price) {
        shoppingCarBottomJieSuanViewPrice.setText("¥" + price );
    }

    @Override
    public void selectALL(boolean so) {
        shoppingCarBottomViewJieSuanCheckBox.setSelected(so);
        shoppingCarBottomViewCaoZuoSellectAll.setSelected(so);
    }


    //商品sku弹窗
    private void commonPop(View view) {
//        initPop(View view);
        //      view.findViewById(R.id.)
        popupWindow = new PopupWindow(view, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT, true);
        // 产生背景变暗效果
        WindowManager.LayoutParams lp = getWindow()
                .getAttributes();
        lp.alpha = 0.4f;
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        getWindow().setAttributes(lp);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchable(true);
        //从下往上弹出动画效果
        popupWindow.setAnimationStyle(R.style.bottomPopupAnimation);
        popupWindow.showAtLocation(findViewById(R.id.shoppingCar_Top), Gravity.BOTTOM, 0, 0);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

            // 在dismiss中恢复透明度
            public void onDismiss() {
                WindowManager.LayoutParams lp = getWindow()
                        .getAttributes();
                lp.alpha = 1f;
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                getWindow().setAttributes(lp);
            }
        });
        //取消
        skuPop_false.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });


    }

    @Override
    protected void onResume() {
                loadData();
        super.onResume();
    }

    //获取购物车列表数据
    private void loadData() {
        shoppingCarTitleBianJi.setText("编辑");
        if (SharedPreferencesUtil.isLogin()) {
            islogin = true;
            ShoppingCarListApi shoppingCarListApi = new ShoppingCarListApi(this, simpleOnNextListener, SharedPreferencesUtil.getLoginToken(), DeviceUtils.getIMEString(ShoppingCarActivity.this));
            shoppingCarListApi.setCache(false);

            HttpManager.getInstance().doHttpDeal(shoppingCarListApi);

            loading_Layout.setOnReloadListener(new LoadingLayout.OnReloadListener() {
                @Override
                public void onReload(View v) {
                    HttpManager.getInstance().doHttpDeal(shoppingCarListApi);
                    loading_Layout.setStatus(LoadingLayout.LOADING);
                }

                @Override
                public void onReturn(View v) {
                    onBackPressed();
                }
            });
            loading_Layout.setStatus(LoadingLayout.LOADING);
        } else {
            islogin = false;
            ShoppingCarListnoLoginApi shoppingCarListApi = new ShoppingCarListnoLoginApi(this, simpleOnNextListener, DeviceUtils.getIMEString(ShoppingCarActivity.this));
            shoppingCarListApi.setCache(false);

            HttpManager.getInstance().doHttpDeal(shoppingCarListApi);

            loading_Layout.setOnReloadListener(new LoadingLayout.OnReloadListener() {
                @Override
                public void onReload(View v) {
                    HttpManager.getInstance().doHttpDeal(shoppingCarListApi);
                    loading_Layout.setStatus(LoadingLayout.LOADING);
                }

                @Override
                public void onReturn(View v) {
                    onBackPressed();
                }
            });
            loading_Layout.setStatus(LoadingLayout.LOADING);
        }
    }
    //获取到焦点时刷新本页面


    //删除购物车
    private void delCart(List<Integer> integerList) {
        String s = new String();
        if (integerList.size() != 0) {
            if (integerList.size() > 1) {
                for (int i = 0; i < integerList.size(); i++) {
                    s += integerList.get(i) + ",";
                }
                for (int i = 0; i < cartList.size(); i++) {
                    if (cartList.get(i).isIsSelect()) {
                        cartList.remove(i);
                        i = -1;
                    }
                }

                Log.e("cjx", "购物车删除成功");
                Goodsadapter.notifyDataSetChanged();
            } else {
                Integer integer = integerList.get(0);
                s = integer + "";
            }
            ShoppingCarDelCartApi delCartApi = new ShoppingCarDelCartApi(ShoppingCarActivity.this, delCartListener,s);
            delCartApi.setCache(false);
            HttpManager.getInstance().doHttpDeal(delCartApi);
            shoppingCarBottomViewJieSuanConstraintLayout.setVisibility(View.VISIBLE);
            shoppingCarConstraintLayoutBottomViewCaoZuo.setVisibility(View.GONE);

        } else {
            ToastTools.showToast("请选择商品");
        }


    }

    //收藏购物车商品
    private void batchAddUserFavorites(int favoritesFlag, List<Integer> skuList,List<Integer> contid) {
        String login_token = SharedPreferencesUtil.getInstance(this).getLogintoken();

        if (login_token != null) {
            String s = new String();
            if (skuList.size() <= 1) {
                Integer integer = skuList.get(0);
                if (favoritesFlag==1){
                    s=integer+"";
                    delCart(contid);
                    for (int i = 0; i < cartList.size(); i++) {
                        if (cartList.get(i).isIsSelect()) {
                            cartList.remove(i);
                            i = -1;
                        }
                    }
                    Goodsadapter.notifyDataSetChanged();
                }else{
                    s=integer+"";
                    delCart(contid);
                }


            } else {
                for (int i = 0; i < skuList.size(); i++) {
                    s += skuList.get(i) + ",";
                }
                delCart(contid);
                for (int i = 0; i < cartList.size(); i++) {
                    if (cartList.get(i).isIsSelect()) {
                        cartList.remove(i);
                        i = -1;
                    }
                }
                Goodsadapter.notifyDataSetChanged();
            }

            ShoppingCarBatchAddUserFavoritesApi batchAddUserFavoritesApi = new ShoppingCarBatchAddUserFavoritesApi(ShoppingCarActivity.this, batchAddUserFavoritesListener, 6, s, login_token);
            batchAddUserFavoritesApi.setCache(false);
            HttpManager.getInstance().doHttpDeal(batchAddUserFavoritesApi);
        } else {
            ToastTools.showToast(ShoppingCarActivity.this, "请先登录");
        }

    }

    //更新商品数量
    private void updateCartNum(int num, int cartId) {
        ShoppingCarUpDateCartNumApi upDateCartNumApi = new ShoppingCarUpDateCartNumApi(this, updateCartNumListener, num, cartId);
        upDateCartNumApi.setCache(false);
        HttpManager.getInstance().doHttpDeal(upDateCartNumApi);
    }

    //选择商品sku
    private void changeSku(int cartId, int skuId) {
        ShoppingCarChangeSkuApi changeSkuApi = new ShoppingCarChangeSkuApi(this, changeSkuListener, cartId, skuId);
        changeSkuApi.setCache(false);
        HttpManager.getInstance().doHttpDeal(changeSkuApi);

    }

    //获取购物车列表数据监听
    HttpOnNextListener simpleOnNextListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                ToastTools.showToast(ShoppingCarActivity.this, subjects.getMsg());
                Log.e("cjx", subjects.getMsg());
                return;
            }
            loading_Layout.setStatus(LoadingLayout.SUCCESS);
            bean = (ShoppingCarListBean) subjects.getData();
            Goodsadapter.setBean(bean);
            cartList = bean.getCartList();

            Log.e("cjx", "购物车列表数据加载完成");

            if (cartList.size() == 0) {
                shoppingCarTitleBianJi.setVisibility(View.GONE);
            }else{
                shoppingCarTitleBianJi.setVisibility(View.VISIBLE);
            }
            setViewVisible();
        }

        @Override
        public void onCacheNext(String cache) {
        }

        @Override
        public void onError(final Throwable e) {
            if (e instanceof SocketTimeoutException || e instanceof ConnectException) {
                loading_Layout.setStatus(LoadingLayout.NO_NETWORK);
            } else {
                loading_Layout.setStatus(LoadingLayout.ERROR);
            }
        }
    };
    //删除购物车数据监听
    HttpOnNextListener delCartListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                ToastTools.showToast(ShoppingCarActivity.this, subjects.getMsg());
                Log.e("cjx", subjects.getMsg());
                return;
            }

            if (cartList.size() == 0) {
                shoppingCarBottomJieSuanViewPrice.setText("¥0.0");
                shoppingCarBottomViewJieSuanConstraintLayout.setVisibility(View.GONE);
                shoppingCarConstraintLayoutWeiDL.setVisibility(View.VISIBLE);
                shoppingCarWeiDLTv.setText("目前没有商品，去购物吧");
                shoppingCarWeiDLDengLu.setVisibility(View.GONE);
                shoppingCarTitleBianJi.setVisibility(View.GONE);

            }else{
                shoppingCarBottomViewJieSuanConstraintLayout.setVisibility(View.VISIBLE);
                shoppingCarTitleBianJi.setVisibility(View.VISIBLE);
            }

        }

        @Override
        public void onCacheNext(String cache) {
        }

        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", "删除购物车" + e.getMessage());
        }
    };
    //收藏购物车监听
    HttpOnNextListener batchAddUserFavoritesListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                ToastTools.showToast(context, subjects.getMsg());
                Log.e("cjx", subjects.getMsg());
                return;
            }
            ToastTools.showToast("移入收藏夹成功");
        }

        @Override
        public void onCacheNext(String cache) {
        }

        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", "删除购物车" + e.getMessage());
        }
    };
    //更新商品数量监听
    HttpOnNextListener updateCartNumListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                ToastTools.showToast(ShoppingCarActivity.this, subjects.getMsg());
                Log.e("cjx", subjects.getMsg());
                return;
            }
            Log.e("cjx", "商品数量更新完成");

        }

        @Override
        public void onCacheNext(String cache) {
        }

        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", "更新商品数量" + e.getMessage());
        }
    };
    //选择商品sku监听
    HttpOnNextListener changeSkuListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
//                ToastTools.showToast(ShoppingCarActivity.this, subjects.getMsg());
                Log.e("cjx", subjects.getMsg());
                return;
            }
            Log.e("cjx", "选择商品sku完成");
        }

        @Override
        public void onCacheNext(String cache) {
        }

        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", "选择商品sku" + e.getMessage());
        }
    };

    ///是否有商品被选中
    public boolean isSelectgoods() {
        for (int i = 0; i < cartList.size(); i++) {
            //只要有一个为flase就false
            if (cartList.get(i).isIsSelect()) {
                return true;
            }
        }
        return false;
    }

    public static Intent queryIntent(Context context) {
        Intent intent = new Intent(context, ShoppingCarActivity.class);
        return intent;
    }


}
